#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn=2000, dir[8][2]={{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

int n, m;
bool map[maxn+1][maxn+1];
int dis[maxn+1][maxn+1], que[maxn*maxn+1][2];

int getDis(int sx, int sy, int tx, int ty) {
	if (sx==tx && sy==ty) return 0;
    for (int i=0; i<=n; i++) {
        for (int j=0; j<=m; j++) {
            dis[i][j] = 4*n*m;
        }
    }
	dis[sx][sy]=0, que[1][0]=sx, que[1][1]=sy;
	for (int h=0, t=1; h<t; ) {
		h++;
		int x=que[h][0], y=que[h][1];
		for (int i=0; i<8; i++) {
			int nx=x+dir[i][0], ny=y+dir[i][1];
			if (nx>0 && ny>0 && nx<=n && ny<=m && map[nx][ny] && dis[nx][ny]>dis[x][y]+1) {
				dis[nx][ny]=dis[x][y]+1;
				if (nx==tx && ny==ty) return dis[nx][ny];
				t++;
				que[t][0]=nx, que[t][1]=ny;
			}
		}
	}
	return -1;
}

int main() {
	freopen("forest.in", "r", stdin);
	freopen("forest.out", "w", stdout);

	scanf("%d %d", &n, &m);
	static int p[4][2];
    int sx, sy, x1, x2, y1, y2;
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=m; j++) {
			char c;
			scanf(" %c", &c);
			map[i][j] = c!='X';
			if (c=='*') {
				for (int k=0; k<4; k++) {
					p[k][0]=i, p[k][1]=j;
				}
                sx = i, sy = j;
			}
		}
	}
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (!map[i][j]) {
                if (i!=sx) {
                    x1 = x2 = i;
                    y1 = 1, y2 = j-1;
                } else {
                    y1 = y2 = j;
                    x1 = 1, x2 = i-1;
                }
                break;
            }
        }
    }
    for (int i=x1; i<=x2; i++) {
        for (int j=y1; j<=y2; j++) {
            map[i][j] = false;
        }
    }

    getDis(sx, sy, 0, 0);
    int ans=4*n*m;
    for (int i=x1; i<=x2; i++) {
        for (int j=y1; j<=y2; j++) {
            int u=4*n*m, v=4*n*m;
            for (int t=-1; t<=1; t++) {
                if (x1==x2) {
                    u = min(u, dis[i-1][j+t]);
                    v = min(v, dis[i+1][j+t]);
                } else {
                    u = min(u, dis[i+t][j-1]);
                    v = min(v, dis[i+t][j+1]);
                }
            }
            ans = min(ans, u+v+2);
        }
    }
    printf("%d\n", ans);

    fclose(stdin);
	fclose(stdout);
	return 0;
}
